package com.handels.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Vector;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.handels.actionForm.relPDFActionForm;
import com.handels.dao.UnidadesDAO;

public class relPDFAction extends DispatchAction{
	
	public ActionForward select_action(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	{
		ActionForward actionForward = null;
		HttpSession session = request.getSession(true);
		
		UnidadesDAO unDAO = new UnidadesDAO();
		
		Vector res_unidades = unDAO.listaUnidades();
		session.setAttribute("res_Unidades", res_unidades);
		
		form.reset(mapping, request);
		actionForward = mapping.findForward("relPDFForm");
		return actionForward;
	}
	
	public ActionForward return_action(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	{
		ActionForward actionForward = null;
		HttpSession session = request.getSession(true);
		UnidadesDAO unDAO = new UnidadesDAO();
		Vector res_unidades = unDAO.listaUnidades();
		session.setAttribute("res_Unidades", res_unidades);
	
		form.reset(mapping, request);
		actionForward = mapping.findForward("relAtendimentoForm");
		return actionForward;
	}
	
	
	public ActionForward generate_action(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	{
		ActionForward actionForward = null;
		HttpSession session = request.getSession(true);
		relPDFActionForm relForm = (relPDFActionForm) form;
		String msgError = "";
		
		
		byte[] arquivo = null;  
		 
		File file = new File("D:\\Desenvolvimento\\ControlLine\\relatorios\\relatorioTeste01.pdf");  
		   
		 
		   
		         try {  
		             arquivo = fileToByte(file);  
		             
		             response.setContentType("application/pdf");  
			         response.setContentLength(arquivo.length);  
			         ServletOutputStream ouputStream = response.getOutputStream();  
			         ouputStream.write(arquivo, 0, arquivo.length);  
			         ouputStream.flush();  
			         ouputStream.close();  
		             
		             
		         } catch (Exception e) {  
		             e.printStackTrace();  
		         }  
		   
		
		actionForward = mapping.findForward("relAtendimentoForm");
		return actionForward;
	}


public static InputStream byteToInputStream(byte[] bytes) throws Exception {  
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);  
    return bais;  
}  

public static byte[] fileToByte(File imagem) throws Exception {  
    FileInputStream fis = new FileInputStream(imagem);  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    byte[] buffer = new byte[8192];  
    int bytesRead = 0;  
    while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {  
        baos.write(buffer, 0, bytesRead);  
    }  
    return baos.toByteArray();  
}  
}
